<?php
include_once('./config/init.php');

//判断是否登录
AuthLogin();

$sql = "SELECT * FROM {$pre_}department";
$deplist = all($sql);


//获取列表传递过来的id
$id = isset($_GET['id']) ? $_GET['id'] : 0;
$sql = "SELECT * FROM {$pre_}job WHERE id = $id";
$job = find($sql);

//获取第一个部门信息
$depid = isset($deplist[0]) ? $deplist[0]['id'] : 0;

//ajax post数据
$action = isset($_POST['action']) ? $_POST['action'] : '';

//如果action == 'check' 我就知道他的请求是 ajax 异步请求过来的
if ($action == "check") {
    $success = [
        'result' => false, //成功 或者 失败
        'msg' => '',  //文案信息
    ];

    $nickname = isset($_POST['nickname']) ? trim($_POST['nickname']) : '';

    //判断是否为空
    if (empty($nickname)) {
        $success['result'] = false;
        $success['msg'] = '职位名称不能为空';
    }

    //去查询数据库职位名称是否存在
    $sql = "SELECT * FROM {$pre_}job WHERE `name` = '$nickname' AND depid != '$depid'";
    $result = find($sql);

    //如果不为空，就说明职位存在，不能添加
    if ($result) {
        $success['result'] = false;
        $success['msg'] = '职位已存在，不能注册';
    } else {
        //不存在，可以添加
        $success['result'] = true;
        $success['msg'] = '可以注册';
    }

    //将信息返回给前端 
    //将php的数据转换为json
    echo json_encode($success);
    exit;
}

//判断是否有post表单
if ($_POST) {
    $nickname = isset($_POST['nickname']) ? trim($_POST['nickname']) : '';

    if (empty($nickname)) {
        ShowMsg('职位不能为空');
        exit;
    }

    //判断职位是否被注册
    $sql = "SELECT * FROM {$pre_}job WHERE `name` = '$nickname' AND depid != '$depid'";
    $check = find($sql);
    if ($check) {
        ShowMsg('职位已存在');
        exit;
    }

    //不存在就可以注册

    //封装数据
    $data = [
        'name' => trim($_POST['nickname']),
        'depid' => trim($_POST['depid']),
    ];

    //执行更新语句
    $affect = update('job', $data, "id = $id");
    if($affect){
        ShowMsg('编辑职位成功','job_list.php');
        exit;
    }else{
        ShowMsg('编辑职位失败');
        exit;
    }
}


?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('meta.php'); ?>
</head>

<body>
    <?php include_once('header.php'); ?>

    <?php include_once('menu.php'); ?>
    <div class="content">
        <div class="header">
            <h1 class="page-title">编辑职位</h1>
        </div>

        <ul class="breadcrumb">
            <li>
                <a href="index.php">控制台</a>
                <span class="divider">/</span>
            </li>
            <li>
                <a href="job_list.php">职位列表</a>
                <span class="divider">/</span>
            </li>
            <li class="active">编辑职位</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='job_list.php'">
                        <i class="icon-list"></i>返回职位列表
                    </button>
                </div>

                <!-- 表单 -->
                <div class="well">
                    <div id="myTabContent" class="tab-content">
                        <div class="tab-pane active in" id="home">
                            <form method="post" enctype="multipart/form-data">
                                <label>职位名称</label>
                                <input type="text" name="nickname" class="input-xxlarge" placeholder="请输入职位名称" required value="<?php echo $job['name'];?>" />
                                <span id="notice" style="display:none;"></span>

                                <label>所属部门</label>
                                <select name="depid" required class="input-xlarge">
                                    <?php foreach ($deplist as $item) { ?>
                                        <option <?php echo $job['depid'] == $item['id'] ? 'selected' : '';?> value="<?php echo $item['id']; ?>"><?php echo $item['name']; ?></option>
                                    <?php } ?>
                                </select>

                                <label></label>
                                <input class="btn btn-primary" type="submit" value="提交" />
                            </form>
                        </div>
                    </div>
                </div>

                <footer>
                    <hr>
                    <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                </footer>
            </div>
        </div>
    </div>
</body>

</html>
<script>
    $("input[name=nickname]").change(function() {
        var nickname = $(this).val()

        //发送一个异步请求，到php服务器里面去
        $.ajax({
            //请求类型
            type: 'post',
            url: 'job_add.php',
            data: {
                action: 'check',
                nickname: nickname
            },
            dataType: 'json',
            success: function(success) {
                if (success.result) {
                    //可以注册
                    $("#notice").css({
                        display: 'inline',
                        color: 'green'
                    })

                    $("#notice").text(success.msg)
                } else {
                    //不能注册
                    $("#notice").css({
                        display: 'inline',
                        color: 'red'
                    })

                    $("#notice").text(success.msg)
                }
            }
        })
    })
</script>